//
//  BCNetworkUtil.swift
//  Blockfolio
//
//  Created by Maybe on 09/05/2021.
//

import Foundation
import Alamofire

let FORMAT_ERROR_TIP = "数据解析错误"

enum ResponseStatus : Int {
    case success = 200   //请求成功
    case failure = 400    //请求失败
}

//enum NetworkStatus : Int {
//    case unknown          = -1  //未知网络
//    case notReachable     = 0    //无连接
//    case wwan             = 1    //蜂窝连接
//    case wifi             = 2    //wifi连接
//
//}

typealias BCResponseSuccess = (_ response: String) -> Void
typealias BCResponseFail = (_ error: String) -> Void
//typealias BCNetworkStatus = (_ NetworkStatus: Int32) -> Void
typealias BCProgressBlock = (_ progress: Int32) -> Void



typealias ResponseListener = (_ status: ResponseStatus, _ response: [String : Any]?, _ messsage: String) ->Void



class BCNetworkUtil: NSObject {
    static let shared = BCNetworkUtil()
    fileprivate var manager : Session
    private override init() {
        let config = URLSessionConfiguration.default
        config.timeoutIntervalForRequest = 60.0
        manager = Session.init()
    }
    
    public func BCPost(url :Api , params :[String : Any]? , success : @escaping BCResponseSuccess, error: @escaping BCResponseFail){
        requestWith(url: url, httpMethod: 0, params: params, success: success, error: error)
    }
    
    public func BCGet(url :Api, params :[String : Any]? , success: @escaping BCResponseSuccess, error: @escaping BCResponseFail){
        requestWith(url: url, httpMethod: 1, params: params, success: success, error: error)
    }
    
    private func requestWith(url: Api, httpMethod: Int,params:[String :Any]?,success: @escaping BCResponseSuccess, error: @escaping BCResponseFail){
        if httpMethod == 0 {
            Post(url: url, params: params, success: success, error: error)
        }else {
            Get(url: url, params: params, success: success, error: error)
        }
    }
    
    private func Post(url: Api, params: [String: Any]?, success: @escaping BCResponseSuccess, error: @escaping BCResponseFail){
       // var header = HTTPHeaders()  // do somthing
        AF.request(url.urlString, method: .post, parameters: params, encoding: URLEncoding.default).response { (response) in
            print(response)
            BClog(response)
            switch response.result {
            case .success:
                let json = String(data: response.data!, encoding: String.Encoding.utf8)
                success(json ?? "")
            case .failure:
                let statusCode = response.response?.statusCode
                BClog(statusCode as Any)
            }
            
        }
    }
    
    private func Get(url : Api, params: [String : Any]?, success: @escaping BCResponseSuccess, error : @escaping BCResponseFail){
        BClog("请求地址\(url.urlString)")
        AF.request(url.urlString,method: .get,parameters: params,encoding: URLEncoding.default).response { (response) in
            print(response)
            BClog(response)
            switch response.result {
            case .success:
                let json = String(data: response.data!, encoding: String.Encoding.utf8)
//                let data = json?.data(using: String.Encoding.utf8)
                success(json ?? "")
            case .failure:
                let statusCode = response.response?.statusCode
                BClog(statusCode as Any)
            }
        }
    }
}
